var myLocalStorage = {
    //exp={expires:0} 当小于365时 为天 否则为毫秒
    set: function(key, value, exp) {
        var obj = new Object()
        var expires = exp.expires || null;

        obj.value = value;
        obj.time = new Date().getTime();

        // expires 类型
        if (expires === null) {
            delete obj.expires;
        } else if (typeof expires === "object") {
            obj.expires = expires.getTime() - obj.time;
        } else if (typeof expires === "number") {
            if ((expires | 0) !== expires) {
                throw new Error("expires must be integer number!");
            }

            // expires 小于 365、366 则，按天算。否则按时间戳算
            if (expires <= getYearDays() && expires > 0) {
                obj.expires = expires * 1000 * 60 * 60 * 24;
            } else if (expires > getYearDays()) {
                obj.expires = expires;
            } else if (expires <= 0) {
                this.removeItem(key);
            }
        }
        localStorage.setItem(key, JSON.stringify(obj));
    },
    get: function(key) {
        var obj = JSON.parse(window.localStorage.getItem(key));
        console.log('Huancun',obj)
        if (obj === "null" || obj === null) return null;
        var expires = obj.expires, 
            now = new Date().getTime(), 
            time = obj.time;
            console.log('当前时间',now - time,expires)
        if (now - time >= expires || now < time) {
            localStorage.removeItem(key);
            return null;
        } else {
            return obj.value;
        }
    },
    // 删除成功返回 true， 否则返回 false
    del: function(key) { 
        
        // if (localStorage.getItem(key) !== null) {
            window.localStorage.removeItem(key)

        //     return this.getItem(key) === null ? true : false;
        // }
        // return true;
    },
    keys: function(key) {
    }
}
function SetDBOfTime(_key,_data,_time) {
    _data=(typeof _data!="string")?JSON.stringify(_data):_data;

    window.localStorage.setItem(_key,_data);

    var nowDate=new Date();
    nowDate.setSeconds(nowDate.getSeconds()+_time)
    window.localStorage.setItem(_key+'-time',base.formatTime(nowDate,'yyyy-MM-dd HH:mm:ss'));
}
function GetDBOfTime(_key) {
    var data=window.localStorage.getItem(_key);
    if(!data) {
        window.localStorage.removeItem(_key)
        return data;
    }
    //获取时间
    var time=window.localStorage.getItem(_key+'-time') || '';
    if(!time || time.length<=0) {
        window.localStorage.removeItem(_key)
        return null;
    }
    var nowDate=new Date();
    nowDate=base.formatTime(nowDate,'yyyy-MM-dd HH:mm:ss')
    if(nowDate>time) {
        return data;
    } else {
        window.localStorage.removeItem(_key)
        return null;
    }
}

function getYearDays () {
    return new Date().getFullYear() % 4 === 0 ? 366 : 365;
}
export default myLocalStorage